class xG_Missions_Info extends Object;

//=============================================================================
// Variables
//=============================================================================
var private x_GameInfo							mGameInfo;
var private xDB_Manager			                mDatabaseManager;
var private xDB_DLLAPI                          mDLLAPI;


/** all mission data here*/
struct SMissionInfo
{
	var Rotator Player_Rotation;
	var Vector MarkerLocation;
	var string MapName;
	//var vector2D Window_Coordinate;
	var string MissionTitle;
	var string MissionLocation;
	var string MissionText;
};

var public SMissionInfo                         MissionInfo;

//=============================================================================
// Functions
//=============================================================================
function Initalize(x_GameInfo aGameInfo)
{
	mGameInfo = aGameInfo;
	mDatabaseManager = mGameInfo.getDBMgr();
	mDLLAPI = mDatabaseManager.getDLLAPI();
}
//=============================================================================
// Functions
//=============================================================================
/** get saved mission index */
function bool GetNextMissionIndex(out int aMissionProgress)
{
	mDLLAPI.SQL_selectDatabase(mDatabaseManager.mGameplayDatabaseIdx);

	if (mDLLAPI.SQL_queryDatabase("SELECT * FROM GAME_DATA WHERE ID = 0;")) //ID always should be 0
	{
		while(mDLLAPI.SQL_nextResult())
		{
			mDLLAPI.SQL_getIntVal("ID_MISSION_PROGRESS", aMissionProgress);
		}
		return true;
	}
	`warn("Can't get next mission index");
	return false;
}

/** get saved mission info and loads it  from DB */
function GetMissionInfo(int aMissionIndex)
{
	local string lRotationStr, lLocationStr, lMapName;
	local string lMTitle, lMLoc, lMText;
	local Rotator lRotation;
	local Vector lLocation;

	mDLLAPI.SQL_selectDatabase(mDatabaseManager.mContentDatabaseIdx);

	if (mDLLAPI.SQL_queryDatabase("SELECT * FROM XCOM_MISSIONS WHERE ID="$aMissionIndex$";"))
	{
		`xDB_InitString(lRotationStr, 50);
		`xDB_InitString(lLocationStr, 50);
		`xDB_InitString(lMapName, 50);
		while(mDLLAPI.SQL_nextResult())
		{

			mDLLAPI.SQL_getStringVal("PLAYER_ROTATION", lRotationStr);
			mDLLAPI.SQL_getStringVal("MARKER_LOCATION", lLocationStr);
			mDLLAPI.SQL_getStringVal("MAP_NAME", lMapName);
		}
		`xDB_String2Rot(lRotationStr,lRotation);
		`xDB_String2Vec(lLocationStr,lLocation);
		MissionInfo.Player_Rotation = lRotation;
		MissionInfo.MarkerLocation = lLocation;
		MissionInfo.MapName = lMapName;
	}
	else `warn("Error while getting Mission info");

	mDLLAPI.SQL_selectDatabase(mDatabaseManager.mLocalizationDatabaseIdx);

	if (mDLLAPI.SQL_queryDatabase("SELECT * FROM MISSION_"$aMissionIndex$" WHERE ID = 0;"))
	{
		`xDB_InitString(lMTitle, 50);
		while(mDLLAPI.SQL_nextResult())
		{
			mDLLAPI.SQL_getStringVal(mDatabaseManager.mLanguage, lMTitle);
		}
		MissionInfo.MissionTitle = lMTitle;
	}
	else `warn("Error while getting localized Mission info");

	if (mDLLAPI.SQL_queryDatabase("SELECT * FROM MISSION_"$aMissionIndex$" WHERE ID = 1;"))
	{
		`xDB_InitString(lMLoc, 50);
		while(mDLLAPI.SQL_nextResult())
		{
			mDLLAPI.SQL_getStringVal(mDatabaseManager.mLanguage, lMLoc);
		}
		MissionInfo.MissionLocation = lMLoc;
	}
	else `warn("Error while getting localized Mission info");

	if (mDLLAPI.SQL_queryDatabase("SELECT * FROM MISSION_"$aMissionIndex$" WHERE ID = 2;"))
	{
		`xDB_InitString(lMText, 256);
		while(mDLLAPI.SQL_nextResult())
		{
			mDLLAPI.SQL_getStringVal(mDatabaseManager.mLanguage, lMText);
		}
		MissionInfo.MissionText = lMText;
	}
	else `warn("Error while getting localized Mission info");
}

//=============================================================================
// Default Properties
//=============================================================================
DefaultProperties
{
	
}